package top.minuy.structure.set;

import top.minuy.res.book.BookRes;
import top.minuy.util.helper.FileHelper;
import top.minuy.structure.linear.array.Array;

import java.util.HashSet;
/**
 * @author Minuy
 * @time 11:46
 * @date 2021/9/11
 */
public class Test {
    public static void main(String[] args) {
        double d1,d2;
        LinkedListSet<String> linkedListSet1,linkedListSet2;
        BSTSet<String> bstSet1,bstSet2;
        AVLTreeSet<String> avlTreeSet1,avlTreeSet2;

        linkedListSet1 = new LinkedListSet<>();
        linkedListSet2 = new LinkedListSet<>();

        bstSet1 = new BSTSet<>();
        bstSet2 = new BSTSet<>();

        avlTreeSet1 = new AVLTreeSet<>();
        avlTreeSet2 = new AVLTreeSet<>();

        System.out.println();
        System.out.println("-> LinkedListSet");
        d1=testSet(linkedListSet1,"A Tale of Two Cities",BookRes.A_TALE_OF_TWO_CITIES);
        System.out.println();
        d2=testSet(linkedListSet2,"Pride and Prejudice", BookRes.PRIDE_AND_PREJUDICE);
        System.out.println();
        System.out.println(String.format("Time : %f s, %f s",d1,d2));

        System.out.println();
        System.out.println("-> BSTSet");
        d1=testSet(bstSet1,"A Tale of Two Cities",BookRes.A_TALE_OF_TWO_CITIES);
        System.out.println();
        d2=testSet(bstSet2,"Pride and Prejudice",BookRes.PRIDE_AND_PREJUDICE);
        System.out.println();
        System.out.println(String.format("Time : %f s, %f s",d1,d2));

        System.out.println();
        System.out.println("-> AVLTreeSet");
        d1=testSet(avlTreeSet1,"A Tale of Two Cities",BookRes.A_TALE_OF_TWO_CITIES);
        System.out.println();
        d2=testSet(avlTreeSet2,"Pride and Prejudice",BookRes.PRIDE_AND_PREJUDICE);
        System.out.println();
        System.out.println(String.format("Time : %f s, %f s",d1,d2));
    }

    private static double testSet(Set<String> set,String title,String fileName){

        HashSet<String> s = new HashSet<>();

        System.out.println(title);

        Array<String> array = new Array<>();

        FileHelper.readFile(fileName,array);

        System.out.println("Total words : "+array.getSize());

        long startTime = System.nanoTime();
        for (int i=0;i<array.getSize();i++) {
            set.add(array.get(i));
        }
        long endTime = System.nanoTime();

        for (int i=0;i<array.getSize();i++) {
            s.add(array.get(i));
        }

        System.out.println(String.format("Total different words : custom = %d, system = %d",set.getSize(),s.size()));

        return (endTime-startTime)/1000000000.0;
    }
}
